package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.RecorderListAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.RecorderListModel;
import com.banshuibao.bsb_android.model.RequestBodyModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;
import com.banshuibao.bsb_android.util.IntentManager;

import java.util.List;

public class RecorderListActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener2, View.OnClickListener, AdapterView.OnItemClickListener {

    private PullToRefreshListView pullToRefreshListView;

    private ListView listView;

    private TextView recorder_bt,msg;

    private int startId,endId;

    private RecorderListAdapter adapter;

    private boolean isUp;

    private int rows;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recorder_list);
        initView();
    }

    public void initView(){
        setTitleText("录音记录");
        recorder_bt = findViewById(R.id.menu);
        recorder_bt.setVisibility(View.VISIBLE);
        recorder_bt.setText("添加录音");
        msg = findViewById(R.id.msg);
        pullToRefreshListView = findViewById(R.id.ptrlv);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(this);
        listView = pullToRefreshListView.getRefreshableView();
        listView.setDivider(null);
        recorder_bt.setOnClickListener(this);
        adapter = new RecorderListAdapter(this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        isUp = false;
        getData();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        isUp = false;
        getData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        isUp = true;
        endId = startId+9;
       requestNet();
    }

    public void getData(){
        startId = 1;
        endId = startId+9;
        if(adapter!=null){
            int count = adapter.getCount();
            if(count>10){
                endId += count-10;
            }
        }
        requestNet();
    }

    public void requestNet(){
        showProgressDialog();
        RequestBodyModel model = new RequestBodyModel();
        model.setSinceId(startId);
        model.setMaxId(endId);
        HttpUtil.doPost(Constants.Url.EXPERT_RECORDER_LIST, model, new HttpResponse(this, RecorderListModel.class) {
            @Override
            public void onResult(Object obj) {
                RecorderListModel model = (RecorderListModel) obj;
                List<RecorderListModel.DataBean> list = model.getData();
                rows = model.getRows();
                startId += rows;
                if(isUp){
                    adapter.up(list);
                }else{
                    adapter.down(list);
                }
                if(adapter.getCount()>0){
                    msg.setVisibility(View.GONE);
                    pullToRefreshListView.setVisibility(View.VISIBLE);
                }else{
                    pullToRefreshListView.setVisibility(View.GONE);
                    msg.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                pullToRefreshListView.onRefreshComplete();
                cancelProgressDialog();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.menu:
                IntentManager.goRecorderContentActivity(RecorderListActivity.this);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if(adapter!=null&&adapter.getItem(i-1)!=null){
            RecorderListModel.DataBean dataBean = adapter.getItem(i - 1);
            String status = dataBean.getStatus();
            if("2".equals(status)||"5".equals(status)){
                IntentManager.goRecorderNoPassDetailActivity(RecorderListActivity.this, adapter.getItem(i - 1).getUuid());
            }else {
                IntentManager.goExpertRecorderDetailActivity(RecorderListActivity.this, adapter.getItem(i - 1).getUuid());
            }
        }
    }
}
